package v1

import (
	"bc/dao"
	"bc/service"
	"bc/util"
	"github.com/gin-gonic/gin"
)

type device struct {

}
func (t *device)Create(c *gin.Context){

	var d dao.Device
	var err error

	d.Uid = c.GetInt("uid")
	if v,ok := c.GetPostForm("name");ok{
		d.Name = v
	}
	if v,ok := c.GetPostForm("dtype");ok{

		dtype := util.StrToInt(err,v)
		d.Dtype = dtype

	}
	if v,ok := c.GetPostForm("brand");ok{
		brand := util.StrToInt(err,v)
		d.Brand = brand
	}
	if v,ok := c.GetPostForm("model");ok{
		model := util.StrToInt(err,v)
		d.Model = model
	}
	if v,ok := c.GetPostForm("data_type");ok{
		data_type := util.StrToInt(err,v)
		d.DataType = data_type
	}
	if v,ok := c.GetPostForm("manage_type");ok{
		manage_type := util.StrToInt(err,v)
		d.ManageType = manage_type
	}

	err = service.Device.Create(d)

	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})
}
func (t *device)Delete(c *gin.Context){

	var err error
	w := make(map[string]interface{})
	d_id := util.StrToInt(err,c.Param("d_id"))
	w["d_id"] = d_id

	err = service.Device.Delete(w)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})

}
func (t *device)Update(c *gin.Context){

	var err error
	var d dao.Device

	if v,ok := c.GetPostForm("name");ok{
		d.Name = v
	}
	if v,ok := c.GetPostForm("dtype");ok{

		dtype := util.StrToInt(err,v)
		d.Dtype = dtype

	}
	if v,ok := c.GetPostForm("brand");ok{
		brand := util.StrToInt(err,v)
		d.Brand = brand
	}
	if v,ok := c.GetPostForm("model");ok{
		model := util.StrToInt(err,v)
		d.Model = model
	}
	if v,ok := c.GetPostForm("data_type");ok{
		data_type := util.StrToInt(err,v)
		d.DataType = data_type
	}
	if v,ok := c.GetPostForm("manage_type");ok{
		manage_type := util.StrToInt(err,v)
		d.ManageType = manage_type
	}

	w := make(map[string]interface{})
	w["d_id"] = util.StrToInt(err,c.Param("d_id"))
	err = service.Device.Update(d,w)
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})
}
// @Summary 设备列表数据
// @Description Get JSON
// @Tags 设备/devices
// @Param name query string false "名称"
// @Param dtype query string false "设备类型"
// @Param brand query string false "品牌"
// @Param model query string false "型号"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} util.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/devices [get]
// @Security
func (t *device)Find(c *gin.Context){

	//var (
	//	err error
	//	data []*dao.Device
	//	total int
	//)
	//
	//w := make(map[string]interface{})
	//page := util.StrToInt(err,c.DefaultQuery("page","1"))
	//pageSize := util.StrToInt(err,c.DefaultQuery("pageSize",strconv.FormatInt(util.Config.App.PageSize,10)))
	//
	//data,total,err = service.Device.Find(w,page,pageSize)
	//c.JSON(200,gin.H{
	//	"code":200,
	//	"msg":"success",
	//	"data":data,
	//	"total":total,
	//})
	var data dao.Device
	var err error
	var pageSize = 10
	var pageIndex = 1

	size := c.Request.FormValue("pageSize")
	if size != "" {
		pageSize = util.StrToInt(err, size)
	}

	index := c.Request.FormValue("pageIndex")
	if index != "" {
		pageIndex = util.StrToInt(err, index)
	}
	//主机类型
	dtype := c.Request.FormValue("dtype")
	if dtype != "" {
		data.Dtype = util.StrToInt(err, dtype)
	}
	//品牌
	brand := c.Request.FormValue("brand")
	if brand != "" {
		data.Brand = util.StrToInt(err, brand)
	}
	//型号
	model := c.Request.FormValue("model")
	if model != "" {
		data.Model = util.StrToInt(err, model)
	}
	util.HasError(c, err, "", 500)
	result, count, err := service.Device.Find(data,pageSize,pageIndex)
	//result, count, err := data.GetPage(pageSize, pageIndex)
	util.HasError(c, err, "", -1)

	var mp = make(map[string]interface{}, 3)
	mp["list"] = result
	mp["count"] = count
	mp["pageIndex"] = pageIndex
	mp["pageSize"] = pageSize
	util.PageOK(c, result, count, pageIndex, pageSize, "")
}
var Device = &device{}
